package com.aw.guiguzi.util

import android.util.Base64
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwLog
import com.aw.baselib.util.SPUtils
import com.aw.guiguzi.BuildConfig
import com.aw.guiguzi.bean.UserInfoBean
import com.google.gson.Gson

object AppUserUtil {
    val user: UserInfoBean?
        get() {
            val userString = SPUtils.get("user", "") as String
            var user = Gson().fromJson(userString, UserInfoBean::class.java)
            if (user == null) {
                user = UserInfoBean()
            }
            return user
        }

    fun saveUser(bean: UserInfoBean?) {
        if (null != bean) {
            val userString = Gson().toJson(bean)
            SPUtils.put("user", userString)
        }
    }

    var isBindVehicle = false
    fun clearToken() {
        SPUtils.put("token", "")
    }

    fun saveToken(token: String?) {
        SPUtils.put("token", token)
    }

    val originToken: String
        get() = SPUtils.get("token", "") as String
    val loginToken: String
        get() {
            val origin = "app:app"
            val base64Result = Base64.encodeToString(origin.toByteArray(), Base64.DEFAULT)
            val result = "Basic $base64Result".replace("\n", " ")
            AwLog.d("getToken result: $result")
            return result
        }
    val token: String
        get() = if (AwDataUtil.isEmpty(SPUtils.get("token", "") as String)) {
            ""
        } else SPUtils.get("token", "") as String

    fun saveTemporaryToken(token: String?) {
        SPUtils.put("temporarytoken", token)
    }

    val temporaryToken: String
        get() = if (AwDataUtil.isEmpty(SPUtils.get("temporarytoken", "") as String)) {
            ""
        } else "Bearer " + SPUtils.get("temporarytoken", "")
    @JvmStatic
    val userId: String?
        get() = if (user != null) {
            user!!.id
        } else ""
    var userRemark: String?
        get() = if (user != null) {
            user!!.remark
        } else ""
        set(param) {
            if (user != null) {
                user!!.remark = param
            }
        }
    val userUUID: String?
        get() = if (user != null) {
            user!!.id
        } else ""
    var isFirstUse: Boolean
        get() = SPUtils.get("isFirstUse", true) as Boolean
        set(isFirstUse) {
            SPUtils.put("isFirstUse", isFirstUse)
        }
    var isAgreePrivacy: Boolean
        get() = SPUtils.get("isAgreePrivacy", false) as Boolean
        set(isFirstUse) {
            SPUtils.put("isAgreePrivacy", isFirstUse)
        }

    fun clearUser(isCleanPwd: Boolean) {
        SPUtils.put("user", "")
        SPUtils.put("token", "")
        if (isCleanPwd) {
            SPUtils.put("password", "")
        }
    }

    fun savePwd(pwd: String?) {
        SPUtils.put("pwd", pwd)
    }

    val pwd: String
        get() = SPUtils.get("pwd", "") as String

    fun saveVin(vin: String?) {
        SPUtils.put("vin", vin)
    }

    val vin: String
        get() = SPUtils.get("vin", "") as String

    fun saveUserLat(lat: String?) {
        SPUtils.put("userLat", lat)
    }

    val userLat: String
        get() =//默认天安门纬度
            SPUtils.get("userLat", "39.915119") as String

    fun saveUserLng(lng: String?) {
        SPUtils.put("userLng", lng)
    }

    val userLng: String
        get() =//默认天安门经度
            SPUtils.get("userLng", "116.403963") as String

    fun saveUserAddress(address: String?) {
        SPUtils.put("userAddress", address)
    }

    val userAddress: String
        get() = SPUtils.get("userAddress", "") as String
    val isAdmin: Boolean
        get() = if (AwDataUtil.isEmpty(account) || AwDataUtil.isEmpty(nickname) || AwDataUtil.isEmpty(
                user!!.roles
            ) || user!!.roles!![0] == null || AwDataUtil.isEmpty(
                user!!.roles!![0].id
            )
        ) {
            false
        } else (account == BuildConfig.maccount || account == BuildConfig.maccount2) && user!!.roles!![0].id == BuildConfig.mrole
    val account: String?
        get() = if (user != null) {
            user!!.username
        } else ""
    val nickname: String?
        get() = if (user != null) {
            user!!.nickname
        } else ""

    /**
     * 登录记住密码
     * @param phone
     * @param password
     */
    fun loginRememberToSave(phone: String?, password: String?, isRemember: Boolean) {
        SPUtils.put("rememberPassword", isRemember)
        SPUtils.put("phone", phone)
        if (isRemember) {
            SPUtils.put("password", password)
        }
    }

    /**
     * 退出登录等清除本地保存的记录.
     */
    fun cleanLocalSaveAccountAndPwd() {
        SPUtils.put("phone", "")
        SPUtils.put("password", "")
    }

    val rememberPhone: String
        get() = SPUtils.get("phone", "") as String
    val rememberPwd: String
        get() = SPUtils.get("password", "") as String
    val isRemeberLogin: Boolean
        get() = SPUtils.get("rememberPassword", false) as Boolean
}